package com.example.computer_exam_training_sys.service.impl;

import com.example.computer_exam_training_sys.dto.PracticeSummaryDTO;
import com.example.computer_exam_training_sys.entity.UserPractice;
import com.example.computer_exam_training_sys.entity.UserPracticeDetail;
import com.example.computer_exam_training_sys.mapper.KnowledgePointMapper;
import com.example.computer_exam_training_sys.mapper.UserPracticeDetailMapper;
import com.example.computer_exam_training_sys.mapper.UserPracticeMapper;
import com.example.computer_exam_training_sys.service.UserPracticeService;
import com.example.computer_exam_training_sys.vo.PracticeStatisticsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public  class UserPracticeServiceImpl implements UserPracticeService {

    @Autowired
    private UserPracticeMapper practiceMapper;

    @Autowired
    private UserPracticeDetailMapper detailMapper;

    @Override
    public int insert(UserPractice practice) {
        return practiceMapper.insert(practice);
    }

    @Override
    public List<UserPractice> selectAll() {
        return practiceMapper.selectAll();
    }

    @Override
    public List<UserPractice> selectByUserId(Integer userId) {
        return practiceMapper.selectByUserId(userId);
    }

    @Override
    public List<com.example.computer_exam_training_sys.vo.UserPracticeCourseVO> selectPracticeCourseByUserId(Integer userId) {
        return practiceMapper.selectPracticeCourseByUserId(userId);
    }

    @Override
    public List<com.example.computer_exam_training_sys.vo.UserPracticeCourseVO> selectAllPracticeCourse() {
        return practiceMapper.selectAllPracticeCourse();
    }

    @Override
    public PracticeStatisticsVO getPracticeStatisticsByUserAndPoint(int userId, int pointId) {
        List<UserPractice> practiceList = practiceMapper.selectByUserAndPoint(userId, pointId);

        int totalPracticeTimes = practiceList.size();
        if (totalPracticeTimes == 0) {
            return new PracticeStatisticsVO(0, 0.0);
        }

        int totalCorrect = 0;
        int totalQuestions = 0;

        for (UserPractice practice : practiceList) {
            List<UserPracticeDetail> details = detailMapper.selectByPracticeId(practice.getPracticeId());
            for (UserPracticeDetail d : details) {
                totalQuestions++;
                if (Boolean.TRUE.equals(d.getIsCorrect())) {
                    totalCorrect++;
                }
            }
        }

        // 返回正确率小数形式，比如0.85，方便前端百分比格式化
        double accuracy = totalQuestions > 0 ? ((double) totalCorrect / totalQuestions) : 0.0;

        return new PracticeStatisticsVO(totalPracticeTimes, accuracy);
    }

    // 新增的getPracticeSummary实现
    @Override
    public PracticeSummaryDTO getPracticeSummary(int userId, int pointId) {
        List<UserPractice> practiceList = practiceMapper.selectByUserAndPoint(userId, pointId);

        int practiceCount = practiceList.size();
        int totalQuestions = 0;
        int totalCorrect = 0;

        for (UserPractice practice : practiceList) {
            List<UserPracticeDetail> details = detailMapper.selectByPracticeId(practice.getPracticeId());
            for (UserPracticeDetail detail : details) {
                totalQuestions++;
                if (Boolean.TRUE.equals(detail.getIsCorrect())) {
                    totalCorrect++;
                }
            }
        }

        double accuracy = totalQuestions > 0 ? ((double) totalCorrect / totalQuestions) : 0.0;

        return new PracticeSummaryDTO(practiceCount, accuracy);
    }
    @Override
    public int countDistinctPoints(Integer userId, Integer courseId) {
        return practiceMapper.countDistinctPoints(userId, courseId);
    }
    @Autowired
    private KnowledgePointMapper pointMapper;

    @Override
    public int countPointsByCourse(Integer courseId) {

        return pointMapper.countByCourseId(courseId); // 调用 KnowledgePointMapper
    }

}
